package com.chen.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chen.bean.Category;
import com.chen.bean.Dish;
import com.chen.bean.Setmeal;
import com.chen.common.R;
import com.chen.exception.CustomException;
import com.chen.mapper.CategoryDao;
import com.chen.service.CategoryService;
import com.chen.service.DishService;
import com.chen.service.SetmealService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, Category> implements CategoryService {
    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;


    /**
     * 新增分类
     * @param category
     * @return
     */
    @Override
    public R<String> addCategory(Category category) {
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Category::getName,category.getName());
        Category category1 = this.getOne(wrapper);
        if (category1!=null){
            throw new CustomException("分类名称重复");
        }
        boolean flag = this.save(category);
        return flag?R.success("增加成功"):R.error("增加失败");
    }

    /**
     * 菜品分类 分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public R<Page<Category>> CategoryPage(Integer page, Integer pageSize) {
        Page<Category> categoryPage = new Page<>(page, pageSize);
        Page<Category> pageFind = this.page(categoryPage);
        return R.success(pageFind);
    }

    /**
     * 修改菜品分类
     * @param category
     * @return
     */
    @Override
    public R<String> updateCategory(Category category) {
        if (StringUtils.isBlank(category.getName())){
            throw new CustomException("分类名不能为空");
        }
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Category::getName,category.getName());
        wrapper.ne(Category::getId,category.getId());
        int count = this.count(wrapper);
        if (count>0){
            throw new CustomException("分类名称【"+category.getName()+"】重复");
        }

        boolean flag = this.updateById(category);
        return flag?R.success("菜品分类修改成功"):R.error("菜品分类修改失败");
    }

    /**
     * 删除菜品分类
     * @param categoryId
     * @return
     */
    @Override
    public R<String> deleteCategory(Long categoryId) {
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Dish::getCategoryId,categoryId);
        int count = dishService.count(wrapper);
        if (count>0){
            throw new CustomException("该分类中有菜品和套餐，请删除菜品和套餐后在删除");
        }
        LambdaQueryWrapper<Setmeal> wrapper2 = new LambdaQueryWrapper<>();
        wrapper2.eq(Setmeal::getCategoryId,categoryId);
        int count1 = setmealService.count(wrapper2);
        if (count1>0){
            throw new CustomException("该分类中有菜品和套餐，请删除菜品和套餐后在删除");
        }
        boolean flag = this.removeById(categoryId);
        return flag? R.success("删除成功"): R.error("删除失败");
    }

    /**
     * 查询所有菜品分类
     * @param type
     * @return
     */
    @Override
    public R<List<Category>> categoryList(Integer type) {
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(type!=null,Category::getType,type);
        List<Category> categoryList = this.list(wrapper);
        return R.success(categoryList);
    }


}
